package cn.yeyiu.netonlinevideo.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.yeyiu.netonlinevideo.R;
import cn.yeyiu.netonlinevideo.bean.InfoData;
import cn.yeyiu.netonlinevideo.util.SourceObserver;
import cn.yeyiu.netonlinevideo.bean.TypeData;
import cn.yeyiu.netonlinevideo.fragment.DataFragment;
import cn.yeyiu.netonlinevideo.fragment.GridDataFragment;
import cn.yeyiu.netonlinevideo.fragment.ListDataFragment;
import cn.yeyiu.netonlinevideo.util.DataServer;
import cn.yeyiu.netonlinevideo.bean.SourceData;
import cn.yeyiu.netonlinevideo.bean.PageData;

/**
 * 简单的 包含一个 tab 的数据展示界面
 * <p>
 * 根据接受的视频源执行解析
 * 获取源分类
 * 获取分类视频list
 * 传递封装list至fragment进行渲染显示
 */

public class SingleTabDataListActivity extends AppCompatActivity {

    private ImageView backBtn;
    private TextView backTv;
    private TabLayout tabLayout;
    private ViewPager2 viewPager;

    public SourceData source;

    private Map<String, DataFragment> fragments;

    // page相关
    private ImageView preIv;
    private ImageView nextIv;
    private TextView curTv;
    private TextView maxTv;

    private String curType;
    private DataServer dataGetter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_single_tab_video_list);

        viewInit();
        dataInit();
        eventInit();
        uiInit();
        observeInit();
    }

    void viewInit() {
        backBtn = findViewById(R.id.back_btn);
        backTv = findViewById(R.id.back_tv);
        tabLayout = findViewById(R.id.video_tabs);
        viewPager = findViewById(R.id.video_list_vp);

        preIv = findViewById(R.id.pre_page_btn);
        nextIv = findViewById(R.id.next_page_btn);
        curTv = findViewById(R.id.cur_page_tv);
        maxTv = findViewById(R.id.sum_page_tv);
    }

    void dataInit() {
        // 获取源信息
        source = JSONObject.parseObject(getIntent().getStringExtra("src"), SourceData.class);
        fragments = new HashMap<>();
        dataGetter = new DataServer(this, source.code);
    }

    void observeInit() {
        dataGetter.postTypeList();
    }

    void uiInit() {
        // title
        this.backTv.setText(source.name);
        findViewById(R.id.page_ll).setVisibility(View.VISIBLE);

        viewPager.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL);
    }

    void eventInit() {
        dataGetter.setTypesChangeListener(new SourceObserver.TypesChangeListener() {
            @Override
            public void onTypesChange(List<TypeData> typeList) {
                viewPager.setAdapter(new FragmentStateAdapter(SingleTabDataListActivity.this) {
                    @NonNull
                    @Override
                    public Fragment createFragment(int position) {
                        String type = typeList.get(position).name;

                        // init 对应类型 video list
                        DataFragment fragment;
                        if ("blog".equals(source.type)) {
                            fragment = new ListDataFragment(SingleTabDataListActivity.this);
                        } else {
                            fragment = new GridDataFragment(SingleTabDataListActivity.this);
                        }
                        fragments.put(type, fragment);
                        return fragment;
                    }

                    @Override
                    public int getItemCount() {
                        return typeList.size();
                    }
                });

                // bind tab and vp
                new TabLayoutMediator(tabLayout, viewPager, new TabLayoutMediator.TabConfigurationStrategy() {
                    @Override
                    public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                        tab.setText(typeList.get(position).name);
                    }
                }).attach();
            }
        });

        dataGetter.setDatasChangeListener(new SourceObserver.DatasChangeListener() {
            @Override
            public void onDatasChange(InfoData infoData) {
                // page
                if (infoData.type.equals(curType)) {
                    maxTv.setText(String.valueOf(infoData.maxIndex + 1));
                }

                DataFragment fragment = fragments.get(infoData.type);
                if (fragment != null && infoData.dataList != null) {
                    fragment.infoDataChange(infoData.dataList);
                }
            }
        });

        backBtn.setOnClickListener(v -> {
            finish();
        });

        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                String type = tab.getText().toString();
                curType = type;

                if (dataGetter.hasInit(type)) {
                    fragments.get(type).changed();
                    PageData pageData = dataGetter.getPageData(type);
                    curTv.setText(String.valueOf(pageData.curPage + 1));
                    maxTv.setText(String.valueOf(pageData.maxPage + 1));
                } else {
                    dataGetter.postFirstPageData(type);
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        preIv.setOnClickListener(v -> {
            changePage(false);
        });

        nextIv.setOnClickListener(v -> {
            changePage(true);
        });
    }

    void changePage(boolean next) {
        fragments.get(curType).changing();
        if (next) {
            dataGetter.nextPage(curType);
        } else {
            dataGetter.prePage(curType);
        }
        curTv.setText(String.valueOf(dataGetter.getCurPage(curType)));
    }
}